<?php

namespace app\common\model;

use think\Cache;

/**
 * 骰子模型类
 * Class DivinationTips
 * @package app\common\model
 */
class DivinationTips extends Base
{
    const CACHE_KEY = 'touzi:';
    const CACHE_TIME = 86400 * 7;

    protected $table = 'wechat_backup_07_18_14_43_00.app_touzi_tips';// 定义表名

    /** 获取详情
     * @param string $value
     *  p-1 月亮
     *  s-2 金牛座
     *  h-3 3宫
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getDetail($value = '')
    {
        $result = false;
        if ($this->cache_on >= 1)
        {
            $result = Cache::get(self::CACHE_KEY . $value);
        }
        if ($result === false)
        {
            try
            {
                $result = $this->field('text')->where(['touzi' => $value, 'status' => 0])->findOrFail();
            }
            catch (\Exception $e)
            {
                // 骰子词条 默认值为空
                $result['text'] = '';
                // 错误信息写入日志
                \think\Log::write('获取骰子词条错误:' . '[' . $value . ']', 'error', true);
            }
            $result = json_decode(json_encode($result, true), true);

            if ($this->cache_on >= 1)
            {
                Cache::set(self::CACHE_KEY . $value, $result, self::CACHE_TIME);
            }
        }

        return $result['text'];
    }
}
